import Vue from 'vue'
import Router from 'vue-router'
import login from '@/views/login/'
import layout from '@/views/layout/'
import home from '@/views/home/'

Vue.use(Router)

let router = new Router({
  routes: [
    { 
        path:'/login',
        name:'login',
        component:login,
        meta:{
          keepAlive: false
        },
    },
    { 
      path:'/register',
      name:'register',
      component:() => import('@/views/register/'),
      meta:{
        keepAlive: false
      },
    },
    {
      path: '/',
      component: layout,
      meta:{
        keepAlive: true
      },
      children:[
        { 
            path:'',
            name:'home',
            component:home,
        },
        { 
          path:'foodView',
          name:'foodView',
          component:() => import('@/views/foodView/'),
        },
        { 
          path:'/publishAction',
          name:'publishAction',
          component:() => import('@/views/community/components/publishAction.vue')
        },
        { 
            path:'community',
            name:'community',
            component:() => import('@/views/community/'),
        },
        { 
            path:'user',
            name:'user',
            component:() => import('@/views/user/'),
        }
      ]
    },
    { 
       path:'/detail/:userId/:actionId',
       name:'detail',
       component:() => import('@/views/community/components/detail.vue'),
       meta:{
          keepAlive: false
      },
    },
    { 
        path:'/account',
        name:'account',
        component:() => import('@/views/user/accountCenter'),
    },
    { 
        path:'/thumb',
        name:'thumb',
        component:() => import('@/views/user/thumb'),
    },
    { 
        path:'/fansList',
        name:'fansList',
        component:() => import('@/views/user/fans'),
    },
    { 
      path:'/idolList',
      name:'idolList',
      component:() => import('@/views/user/idols'),
    },

    { 
      path:'/myplan',
      name:'myplan',
      component: () => import('@/views/home/myPlan')
    },
    { 
      path:'/foodDetail/:foodId',
      name:'foodDetail',
      component: () => import('@/components/foodDetail.vue')
    },
    { 
      path:'/menuMoudle',
      name:'menuMoudle',
      component: () => import('@/views/home/menuMoudle')
    },
    { 
      path:'/personCate',
      name:'personCate',
      component: () => import('@/views/home/menuMoudle/personCate/')
    }
  ]
})


router.beforeEach((to,from,next) => { 
  const result = JSON.parse(window.localStorage.getItem("result"))

  if(to.path != '/login' && to.path != '/register'){ 
      if(result){ 
        next()
      } else { 
        next('/login')
      }

  } else {
    next()
    if(Vue.prototype.websocket.getWebSocket() && next.path != '/detail/*'){ 
        Vue.prototype.websocket.forceClose();
    }
    
  }
})

export default router
